package com.zwz.service.impl;

import com.zwz.entity.AgentAccountInfo;
import com.zwz.entity.EnterpriseInfo;
import com.zwz.mapper.AgentAccountInfoMapper;
import com.zwz.service.AgentAccountInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

@Service
@Transactional
public class AgentAccountInfoServiceImpl implements AgentAccountInfoService {

    @Autowired
    private AgentAccountInfoMapper mapper;

    @Override
    public void create(AgentAccountInfo record) {


        EnterpriseInfo enterpriseInfo=new EnterpriseInfo();
        enterpriseInfo.setId(Long.valueOf(record.getEnterpriseId()));

        validateMobile(record.getMobile());


        record.setEnterpriseId(Math.toIntExact(enterpriseInfo.getId()));

        mapper.insert(record);
    }

    @Override
    public AgentAccountInfo getById(Integer id) {
        return mapper.selectById(id);
    }

    @Override
    public void update(AgentAccountInfo record) {
        validateMobile(record.getMobile());
        mapper.update(record);
    }

    @Override
    public void deleteById(Integer id) {
        mapper.deleteById(id);
    }

    @Override
    public List<AgentAccountInfo> getAll() {
        return mapper.selectAll();
    }

    @Override
    public List<AgentAccountInfo> getByHotStatus(Boolean hasHot) {
        return mapper.selectByHotStatus(hasHot);
    }

    private void validateMobile(String mobile) {
        if (mobile != null && !mobile.matches("^\\+?[0-9]{8,20}$")) {
            System.err.println("no valid tel");

        }
    }
}